package com.qzsoft.zhjd.materiel.modules.materielinventory.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.materiel.modules.materielinventory.dto.MaterielInventoryImportDto;
import com.qzsoft.zhjd.materiel.modules.materielinventory.dto.MaterielInventoryListDto;
import com.qzsoft.zhjd.materiel.modules.materielinventory.entity.MaterielInventoryEntity;
import com.qzsoft.zhjd.materiel.modules.materielinventory.service.MaterielInventoryService;

/**
 * @ClassName MaterielInventoryController
 * @Description 物料盘点控制器
 * @author linhuawei
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/api/materielInventory")
public class MaterielInventoryController {

    @Autowired
    private MaterielInventoryService materielInventoryService;

    /**
     * @Title page
     * @Description 获取物料盘点数据分页
     * @param params 查询条件
     * @return 物料盘点数据分页
     */
    @RequiresPermissions("materiel:inventory:page")
    @GetMapping("/page")
    public Result<IPage<MaterielInventoryListDto>> page(@RequestParam Map<String, Object> params) {
        IPage<MaterielInventoryListDto> page = materielInventoryService.pageDto(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title info
     * @Description 获取数据对象
     * @param id 数据主键
     * @return 数据对象
     */
    @RequiresPermissions("materiel:inventory:update")
    @GetMapping("/info/{id}")
    public Result<MaterielInventoryEntity> info(@PathVariable("id") String id) {
        MaterielInventoryEntity entity = materielInventoryService.getById(id);
        if (null == entity) {
            RenExceptionUtils.throwEx(BizCode.DATA_NOT_FOUND, "数据不存在");
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title save
     * @Description 添加数据
     * @param entity 需要添加的数据
     * @return 添加结果
     */
    @RequiresPermissions("materiel:inventory:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MaterielInventoryEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        materielInventoryService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title update
     * @Description 修改数据
     * @param entity 需要修改的数据
     * @return 修改结果
     */
    @RequiresPermissions("materiel:inventory:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody MaterielInventoryEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        materielInventoryService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title delete
     * @Description 删除数据
     * @param id 数据主键
     * @return 删除结果
     */
    @RequiresPermissions("materiel:inventory:delete")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        materielInventoryService.removeById(id);
        return ResultUtils.ok();
    }

    /**
     * @Title template
     * @Description 下载模板
     */
    @RequiresPermissions("materiel:inventory:template")
    @GetMapping("/template")
    public void template(HttpServletResponse response) {
        materielInventoryService.template(response);
    }

    /**
     * @Title importExcel
     * @Description 数据导入
     */
    @RequiresPermissions("materiel:inventory:import")
    @PostMapping("/import")
    public Result<List<MaterielInventoryImportDto>> importExcel(String baseCode, MultipartFile file) {
        if (ObjectUtils.isEmpty(baseCode)) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "基地名称不可为空");
        }
        if (null == file || 0 == file.getSize()) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "上传文件不可为空");
        }
        List<MaterielInventoryImportDto> result = materielInventoryService.importExcel(baseCode, file);
        return ResultUtils.ok(result);
    }
}
